package avicit.bdp.dgs.qa.service;

import avicit.bdp.dgs.qa.utils.constant.QaConstant;
import avicit.platform6.core.context.ThreadContextHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-06-14
 * @类说明：MetricService
 * @修改记录：
 * @注意事项：
 * @主要功能：质量概览Service
 */
@Service
public class MetricService {
    private static final Logger logger = LoggerFactory.getLogger(MetricService.class);

    @Autowired
    private JobService jobService;

    @Autowired
    private JobReportService jobReportService;

    @Autowired
    private RuleService ruleService;

    @Autowired
    private RuleTemplateService ruleTemplateService;

    /**
     * 查看基本指标，包括模板总数、规则总数、任务总数、评估次数、告警次数
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public Map<String, Object> queryBasicMetrics(String startTime, String endTime) {
        Map<String, Object> result = new HashMap<>();
        List<String> wordSecretList = ThreadContextHelper.getWordSecretList();

        // step1:获取规则模板数量
        Long ruleTemplateNum = this.ruleTemplateService.countRuleTemplate(startTime, endTime, wordSecretList);
        result.put(QaConstant.METRIC_RULE_TEMPLATE_NUM, ruleTemplateNum == null ? 0 : ruleTemplateNum);

        // step2:获取质量规则数量
        Long ruleNum = this.ruleService.countRule(startTime, endTime, wordSecretList);
        result.put(QaConstant.METRIC_RULE_NUM, ruleNum == null ? 0 : ruleNum);

        // step3:获取质量作业数量
        Long jobNum = this.jobService.countJob(startTime, endTime, wordSecretList);
        result.put(QaConstant.METRIC_JOB_NUM, jobNum == null ? 0 : jobNum);

        // step4:获取质量评估次数
        Long executeJobNum = this.jobService.countProcessInstance(startTime, endTime);
        result.put(QaConstant.METRIC_EXECUTE_JOB_NUM, executeJobNum == null ? 0 : executeJobNum);

        // step5:获取质量告警次数
        Long alertNum = this.jobReportService.countJobAlert(startTime, endTime, wordSecretList);
        result.put(QaConstant.METRIC_ALERT_NUM, alertNum == null ? 0 : alertNum);

        return result;
    }

    /**
     * 查看质量评分趋势图
     *
     * @param timeMode,时间，0：近24h、1：近7天、2：近30天、3：本月
     * @param startTime
     * @param endTime
     * @return
     */
    public List<Map<String, Object>> queryScoreTrend(Integer timeMode, String startTime, String endTime) {
        List<String> wordSecretList = ThreadContextHelper.getWordSecretList();
        return this.jobReportService.queryScoreTrend(timeMode, startTime, endTime, wordSecretList);
    }

    /**
     * 查看质量告警趋势图
     *
     * @param timeMode,时间，0：近24h、1：近7天、2：近30天、3：本月
     * @param startTime
     * @param endTime
     * @return
     */
    public List<Map<String, Object>> queryAlertTrend(Integer timeMode, String startTime, String endTime) {
        List<String> wordSecretList = ThreadContextHelper.getWordSecretList();
        return this.jobReportService.queryAlertTrend(timeMode, startTime, endTime, wordSecretList);
    }

    /**
     * 查看质量评分分布图
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<Map<String, Object>> queryScoreDistribution(String startTime, String endTime) {
        List<String> wordSecretList = ThreadContextHelper.getWordSecretList();
        return this.jobReportService.queryScoreDistribution(startTime, endTime, wordSecretList);
    }

    /**
     * 查看质量评分排名
     *
     * @param rankMode,排名方式，0：排名最高、1：排名最低"
     * @param startTime
     * @param endTime
     * @return
     */
    public List<Map<String, Object>> queryScoreRank(Integer rankMode, Integer limitNum, String startTime, String endTime) {
        List<String> wordSecretList = ThreadContextHelper.getWordSecretList();
        return this.jobReportService.queryScoreRank(rankMode, limitNum, startTime, endTime, wordSecretList);
    }

    /**
     * 根据作业状态统计数目
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<Map<String, Object>> queryJobStateDistribution(String startTime, String endTime) {
        return this.jobService.queryJobStateDistribution(startTime, endTime);
    }
}
